package com.hy.memoryassistant;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


/**
 * An activity representing a list of Notes. This activity
 * has different presentations for handset and tablet-size devices. On
 * handsets, the activity presents a list of items, which when touched,
 * lead to a {@link NoteDetailActivity} representing
 * item details. On tablets, the activity presents the list of items and
 * item details side-by-side using two vertical panes.
 * <p>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link NoteListFragment} and the item details
 * (if present) is a {@link NoteDetailFragment}.
 * <p>
 * This activity also implements the required
 * {@link NoteListFragment.Callbacks} interface
 * to listen for item selections.
 */
public class NoteListActivity extends Activity
        implements NoteListFragment.Callbacks {
    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;
    private ReminderSQLCollection mCollection;
    private SchedulerReceiver scheduler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set preference to default values on first run
        // subsequent calls do nothing
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        // Add test data to database
		mCollection = ReminderSQLCollection.getInstance(this);

		// createTestData();


        setContentView(R.layout.activity_note_list);

        if (findViewById(R.id.note_detail_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-large and
            // res/values-sw600dp). If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;

            // In two-pane mode, list items should be given the
            // 'activated' state when touched.
            ((NoteListFragment) getFragmentManager()
                    .findFragmentById(R.id.note_list))
                    .setActivateOnItemClick(true);
        }


        // set the scheduler when the list starts, this is for testing only
        // hopefully, the autostart will take care of it once its verified working
        if (scheduler == null) {
            scheduler = new SchedulerReceiver();

            scheduler.SetAlarm(this);
        }
    }

    /**
     * Callback method from {@link NoteListFragment.Callbacks}
     * indicating that the item with the given ID was selected.
     */
    @Override
    public void onItemSelected(Long id) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putLong(NoteDetailFragment.ARG_ITEM_ID, id);
            NoteDetailFragment fragment = new NoteDetailFragment();
            fragment.setArguments(arguments);
            getFragmentManager().beginTransaction()
                    .replace(R.id.note_detail_container, fragment)
                    .commit();

        } else {
            // In single-pane mode, simply start the detail activity
            // for the selected item ID.
            Intent detailIntent = new Intent(this, NoteDetailActivity.class);
            detailIntent.putExtra(NoteDetailFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }
    }
    
	/**
	 * Creates the actions menu
	 */
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.actions, menu);

		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * handles the menu item clicked accordingly
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_add_item:
			Intent newReminder = new Intent(this, EditNoteDetailActivity.class);
			startActivity(newReminder);
			// Toast.makeText(this, "add item", Toast.LENGTH_LONG).show();
			return true;
		case R.id.menu_about:
			Toast.makeText(this, "about", Toast.LENGTH_LONG).show();
			return true;
		case R.id.menu_settings:
            Intent newSettings = new Intent(this, SettingsActivity.class);

            startActivity(newSettings);
            return true;
            default:
			return false;
		}
	}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        NoteListFragment frag = (NoteListFragment) getFragmentManager().findFragmentById(R.id.note_list);
        frag.onResume();
    }


    private void createTestData() {
        long today = System.currentTimeMillis();

        // reminder 1
        Reminder testReminder1 = new Reminder();
        testReminder1.date_added = System.currentTimeMillis();
        testReminder1.ext_link = "ext_link1";
        testReminder1.prompt = "what is a prompt";
        testReminder1.title = "this is a title";
        testReminder1.summary = "i am the best";
        testReminder1.tag = "TAG";
        testReminder1.media_type = 1;
        testReminder1.next_notification = today;
        testReminder1.notification_count = 1;

        // reminder 2
        Reminder testReminder2 = new Reminder();
        testReminder2.date_added = System.currentTimeMillis();
        testReminder2.ext_link = "ext_link2";
        testReminder2.prompt = "what is a prompt2";
        testReminder2.title = "this is a title2";
        testReminder2.summary = "i am the best2";
        testReminder2.tag = "TAG2";
        testReminder2.media_type = 2;
        testReminder2.next_notification = 0;
        testReminder2.notification_count = 2;

        // reminder 3
        Reminder testReminder3 = new Reminder();
        testReminder3.date_added = System.currentTimeMillis();
        testReminder3.ext_link = "ext_link3";
        testReminder3.prompt = "what is a prompt3";
        testReminder3.title = "this is a title3";
        testReminder3.summary = "i am the bes3t";
        testReminder3.tag = "TAG3";
        testReminder3.media_type = 3;
        testReminder3.next_notification = 0;
        testReminder3.notification_count = 3;

        mCollection.removeAll();
        mCollection.add(testReminder1);
        mCollection.add(testReminder2);
        mCollection.add(testReminder3);
    }

}
